package com.atguigu.eduService.controller;


import com.atguigu.common.Result;
import com.atguigu.eduService.entity.EduTeacher;
import com.atguigu.eduService.entity.vo.TeacherQuery;
import com.atguigu.eduService.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author smday
 * @since 2020-07-25
 */

@Api(description = "讲师管理")
@RestController
@RequestMapping("/edu/teacher")
//@CrossOrigin
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;

    //查询讲师表所有数据
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/findAll")
    public Result findAllTeacher() {

        return Result.ok().data("items", teacherService.list(null));

    }

    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("/{id}")
    public Result deleteTeacher(@PathVariable("id") String id) {
        return teacherService.removeById(id) ? Result.ok() : Result.error();
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping("/{id}")
    public Result add(@PathVariable("id") String id){
        EduTeacher teacher = teacherService.getById(id);
        return Result.ok().data("teacher",teacher);
    }

    @ApiOperation(value = "修改信息")
    @PostMapping("/update")
    public Result update(@RequestBody EduTeacher teacher){
        return teacherService.updateById(teacher)?Result.ok():Result.error();
    }



    @ApiOperation(value = "分页查询信息")
    @GetMapping("/getPage")
    public Result getPage(
            @ApiParam(name = "current", value = "当前页码数", required = true)
            @RequestParam("current") Integer current,
            @ApiParam(name = "size", value = "每页记录数", required = true)
            @RequestParam("size") Integer size) {

        Page<EduTeacher> page = new Page<>(current, size);

        teacherService.page(page, null);
        Result result = Result.ok();
        result.data("total", page.getTotal());
        result.data("rows", page.getRecords());
        return result;

    }

    @ApiOperation(value = "分页条件查询信息")
    @PostMapping("/getPageExample")
    public Result getPageExample(@ApiParam(name = "current", value = "当前页码数", required = true)
                                 @RequestParam("current") Integer current,
                                 @ApiParam(name = "size", value = "每页记录数", required = true)
                                 @RequestParam("size") Integer size,
                                 @RequestBody(required = false) TeacherQuery teacherQuery) {


        IPage<EduTeacher> page = teacherService.pageQuery(current, size, teacherQuery);
        long total = page.getTotal();
        List<EduTeacher> records =
                page.getRecords();
        return Result.ok().data("total", total).data("rows", records);
    }
    @ApiOperation(value = "增加讲师")
    @PostMapping("/add")
    public Result add(@RequestBody EduTeacher eduTeacher){

        return teacherService.save(eduTeacher)?Result.ok():Result.error();

    }

}

